/*
 * @Author: your name
 * @Date: 2021-10-26 22:03:41
 * @LastEditTime: 2021-11-15 21:50:09
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue-vben-admin-main\src\logics\initAppConfig.ts
 */
/**
 * Application configuration
 */
import type { ProjectConfig } from '/#/config';

import { PROJ_CFG_KEY } from '/@/enums/cacheEnum';
import projectSetting from '/@/settings/projectSetting';

import { updateHeaderBgColor, updateSidebarBgColor } from '/@/logics/theme/updateBackground';
import { updateColorWeak } from '/@/logics/theme/updateColorWeak';
import { updateGrayMode } from '/@/logics/theme/updateGrayMode';
import { updateDarkTheme } from '/@/logics/theme/dark';
import { changeTheme } from '/@/logics/theme';

import { useAppStore } from '/@/store/modules/app';
import { useLocaleStore } from '/@/store/modules/locale';

import { getCommonStoragePrefix, getStorageShortName } from '/@/utils/env';

import { primaryColor } from '../../build/config/themeConfig';
import { Persistent } from '/@/utils/cache/persistent';
import { deepMerge } from '/@/utils';
import { ThemeEnum } from '/@/enums/appEnum';

// Initial project configuration
export function initAppConfigStore() {
    // 区域文字设置
    const localeStore = useLocaleStore();
    // app的全局页面设置
    const appStore = useAppStore();
    // 先从缓存中获取配置,如果没有的话再从默认配置中获取,并设计了合并的函数
    let projCfg: ProjectConfig = Persistent.getLocal(PROJ_CFG_KEY) as ProjectConfig;
    projCfg = deepMerge(projectSetting, projCfg || {});
    const darkMode = appStore.getDarkMode;
    const {
        colorWeak,
        grayMode,
        themeColor,

        headerSetting: { bgColor: headerBgColor } = {},
        menuSetting: { bgColor } = {},
    } = projCfg;
    try {
        if (themeColor && themeColor !== primaryColor) {
            changeTheme(themeColor);
        }

        grayMode && updateGrayMode(grayMode);
        colorWeak && updateColorWeak(colorWeak);
    } catch (error) {
        console.log(error);
    }
    appStore.setProjectConfig(projCfg);

    // init dark mode
    updateDarkTheme(darkMode);
    if (darkMode === ThemeEnum.DARK) {
        updateHeaderBgColor();
        updateSidebarBgColor();
    } else {
        headerBgColor && updateHeaderBgColor(headerBgColor);
        bgColor && updateSidebarBgColor(bgColor);
    }
    // init store
    localeStore.initLocale();

    setTimeout(() => {
        clearObsoleteStorage();
    }, 16);
}

/**
 * As the version continues to iterate, there will be more and more cache keys stored in localStorage.
 * This method is used to delete useless keys
 */
export function clearObsoleteStorage() {
    const commonPrefix = getCommonStoragePrefix();
    const shortPrefix = getStorageShortName();

    [localStorage, sessionStorage].forEach((item: Storage) => {
        Object.keys(item).forEach((key) => {
            if (key && key.startsWith(commonPrefix) && !key.startsWith(shortPrefix)) {
                item.removeItem(key);
            }
        });
    });
}
